package com.cskaoyan.service;

import com.cskaoyan.bean.GenericData;
import com.cskaoyan.bean.Systempermissions;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketRole;
import com.cskaoyan.bean.po.MarketRoleExample;
import com.cskaoyan.bean.role.SimpleRole;
import com.cskaoyan.mapper.MarketRoleMapper;
import com.cskaoyan.mapper.RoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    MarketRoleMapper marketRoleMapper;

    @Autowired
    RoleMapper roleMapper;

    @Override
    public GenericData<MarketRole> query(BaseParam param, String name) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        MarketRoleExample marketRoleExample = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = marketRoleExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if(name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        marketRoleExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(marketRoleExample);
        PageInfo pageInfo = new PageInfo(marketRoles);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return GenericData.data(total,pages,param.getLimit(), param.getPage(), marketRoles);
    }

    @Override
    public Integer create(String name, String desc) {
        MarketRole marketRole = new MarketRole();
        marketRole.setName(name);
        marketRole.setDesc(desc);
        marketRole.setId(null);
        Date date = new Date();
        marketRole.setUpdateTime(date);
        marketRole.setAddTime(date);
        marketRole.setEnabled(true);
        marketRole.setDeleted(false);
        int insertSelective = marketRoleMapper.insertSelective(marketRole);
        return marketRole.getId();
    }

    @Override
    public MarketRole queryById(Integer id) {
        MarketRole marketRole = marketRoleMapper.selectByPrimaryKey(id);
        return marketRole;
    }

    @Override
    public Integer update(MarketRole marketRole) {
        marketRole.setDeleted(false);
        // marketRole.setEnabled(true);
        marketRole.setUpdateTime(new Date());
        int update = marketRoleMapper.updateByPrimaryKeySelective(marketRole);
        return update;
    }

    @Override
    public Integer deleted(MarketRole marketRole) {
        marketRole.setDeleted(true);
        marketRole.setName(null);
        marketRole.setUpdateTime(new Date());
        marketRole.setEnabled(null);
        marketRole.setDesc(null);
        marketRole.setAddTime(null);
        int deleted = marketRoleMapper.updateByPrimaryKeySelective(marketRole);
        return deleted;
    }

    @Override
    public GenericData<SimpleRole> options() {
        List<SimpleRole> simpleRoles = roleMapper.selectOptions();
        return GenericData.data(simpleRoles.size(),1,simpleRoles.size(), 1, simpleRoles);
    }

    @Override
    public List<Systempermissions> querySystempermissions() {
        List<Systempermissions> systempermissions = roleMapper.selectSystempermissions();
        return systempermissions;
    }

    @Override
    public List<String> queryRolePermissions(Integer roleId) {
        List<String> permissions = roleMapper.selectRoleRolePermissions(roleId);
        return permissions;
    }

    @Override
    public Integer deletePermissionsByRoleId(Integer roleId) {
        if(roleId == null){
            return null;
        }
        Integer update = roleMapper.updateDeletedByRoleId(roleId);
        return update;
    }

    @Override
    public Integer insertPermissionsByRoleId(Integer roleId, String permission) {
        Integer insert = roleMapper.insertPermissionsByRoleId(roleId, permission);
        return insert;
    }

}
